﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Server.Models;
using Server.UserService;
using CryptUtil;
using Box51;
using MessageModel;

namespace Server.Users
{
    public class CUserBLL
    {
        CUserDAL cDal = new CUserDAL();
        BoxProtocol box = new BoxProtocol();
        /// <summary>
        /// 生存令牌
        /// </summary>
        /// <returns></returns>
        public string GetToken()
        {
            try
            {
                return Crypt.MD32Crypt(Guid.NewGuid().ToString(), DateTime.Now.Millisecond.ToString(), Encoding.UTF8);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userModel"></param>
        /// <returns></returns>
        public UserLoginModel UserLogin(UserInfo userModel)
        {
            try
            {
                if (null == userModel)
                    throw new Exception("登录信息不能为空!");
                if (string.IsNullOrEmpty(userModel.LoginName))
                    throw new Exception("帐号不能为空!");
                if (string.IsNullOrEmpty(userModel.Password))
                    throw new Exception("密码不能为空!");
                //通过用户名获取用户信息
                UserInfo user = cDal.GetUserByName(userModel.LoginName);
                if (null == user)
                    throw new Exception("用户不存在!");
                //判断密码是否正确
                string pass = CryptUtil.Crypt.MD32Crypt(userModel.Password, user.Sault, Encoding.UTF8);
                if (pass != user.Password)
                    throw new Exception("密码错误!");
                string token = this.GetToken();
                cDal.SaveOnline(token, user.Id);
                user.Password = "";
                user.Sault = "";
                //返回信息 1. 用户信息 2.在线Token 3.税控信息SKQueryOutputModel
                //1.用户信息
                UserLoginModel ul = new UserLoginModel();
                ul.user = user;
                //2.在线Token
                ul.Token = token;
                //3.获取设备信息
                var dt = cDal.GetDevice(user.DeviceId);
                if (dt.Rows.Count < 1)
                    throw new Exception("未找到对应的设备");
                var dr = dt.Rows[0];
                DeviceInfoModel dm = new DeviceInfoModel()
                {
                    id = Convert.ToInt32(dr["id"]),
                    DeviceSession = dr["DeviceSession"].ToString(),
                    SessionExpired = Convert.ToDateTime(dr["SessionExpired"]),
                    DeviceIP = dr["DeviceIP"].ToString(),
                    Number = Convert.ToInt32(dr["Number"]),
                    Remark = dr["Remark"].ToString()
                };
                //4.登录
                box.ServerAddress = dm.DeviceIP;
                //LoginReturn lr = box.LoginToDevice(user.TaxNum);
                //5.保存密钥
                //cDal.SaveDeviceSession(lr.SQMY,user.DeviceId);
                //6.税控信息
                SKQueryOutputModel skOut = new SKQueryOutputModel();
                SKQueryRequestModel sk = new SKQueryRequestModel()
                {
                    NSRSBH = user.TaxNum,
                    FJH = dm.Number.ToString()
                };
                skOut = box.GetTaxInfo(sk, user.TaxNum, dm.DeviceSession);
                if (skOut.ReturnData == null)
                    throw new Exception("解析错误!");
                ul.sk = skOut.ReturnData;
                return ul;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public bool CheckUser(string Token)
        {
            try
            {
                return cDal.CheckUser(Token);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 检查用户时 通过令牌获取用户信息
        /// </summary>
        /// <param name="Token"></param>
        /// <returns></returns>
        public UserInfo GetUserByToken(string Token)
        {
            try
            {
                if (string.IsNullOrEmpty(Token))
                    throw new Exception("令牌为空!");
                return cDal.GetUserByToken(Token);
            }
            catch (Exception)
            {
                throw;
            }
        }



    }
}
